<?php
/**
 * @ClassName: Cronjob
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-10 11:08
 * @Version: V1.0.0
 */

namespace app\crontab\controller;

use app\common\enum\OrderStatus;
use app\common\enum\PayStatus;
use app\common\enum\ShippingStatus;
use app\common\logic\DateTimeLogic;
use app\common\model;
use think\Controller;

class Cronjob extends Controller
{
    /**
     * 定时任务测试入口
     * @return void
     */
    function test()
    {
        makelog('---定时任务测试---start---' . time(), 'crontab/cronjob');
        $list = model\Member::select();
        foreach ($list as $item) {
            sleep(rand(1, 5)); // 暂停1-5秒
        }
        makelog('---定时任务测试---end---' . time(), 'crontab/cronjob');
    }

    function starratingUpgrade(){
        $dateTimeLogic = new DateTimeLogic();
        // 近一月
        $dateArray = $dateTimeLogic->handleStartStopDate(-31);

        $customer_list = model\Customer::select();
        foreach ($customer_list as $item) {
            $where = [];
            $where[] = ['o.customer_id', '=', $item['id']];//客户ID
            $where[] = ['o.pay_status', '=', PayStatus::pay];//已支付
            $where[] = ['o.shipping_status', '=', ShippingStatus::shipped];//已配送
            $where[] = ['o.order_status', 'in', [OrderStatus::WAITCCOMMENT, OrderStatus::FINISH]];//已收货、已完成
            $order_list = model\ShopOrder::alias('o')
                ->where($where)
                ->fieldRaw("(select total_num from gyd_shop_order_goods og where og.order_id=o.id) as total_num")
                ->whereBetween('o.pay_time', [$dateArray['startDate'], $dateArray['endDate']])
                ->sum('total_num');

            $barrelage = 0;
            foreach ($order_list as $order) {
                $barrelage += $order['total_num'];
            }
            $starLevelList = model\CustomerStarLevel::where('status', 1)->select();
            $star_level = 1;
            foreach ($starLevelList as $level) {
                if (($level['frame_start'] == -1 || $barrelage >= $level['frame_start']) && ($barrelage <= $level['frame_end'] || $level['frame_end'] == -1)) {
                    $star_level = $level['level'];
                    break;
                }
            }

            if ($star_level != $item['star_level']) {
                model\Customer::where('id', $item['id'])->update([
                    '$star_level' => $star_level,
                    'quality' => $star_level,
                ]);
                $desc = "客户：" . $item['customer_name'] . "在" . $dateArray['startDate'] . "至" . $dateArray['endDate'] . "期间总订水" . $barrelage . "桶，客户优质度变更为" . $star_level . "星";
                $data = [
                    'customer_id' => $item['id'],
                    'desc' => $desc,
                    'change' => $star_level
                ];
                model\CustomerLog::insert($data);
            }
        }
    }

    /**
     * 注销未实名认证代理
     * @return void
     */
    function logoutAgent()
    {
        makelog('---注销未实名认证代理---start---' . time(), 'crontab/cronjob');
        $list = model\Agent::where('realname', '')->whereOr('id_card_num', '')->select();
        $config = web_config('agent_config');
        foreach ($list as $item) {
            $arr = calc_time_diff($item['create_time'], date_now());
            if ($arr['day'] >= $config['agent_logout_deadline']) {
                model\Agent::where('id', $item['id'])->update([
                    'update_time' => date_now(),
                    'delete_time' => date_now(),
                ]);
            }
        }
        makelog('---注销未实名认证代理---end---' . time(), 'crontab/cronjob');
    }

    /**
     * 注销未填写资料商户
     * @return void
     */
    function logoutMerchant()
    {
        makelog('---注销未填写资料商户---start---' . time(), 'crontab/cronjob');
        $list = model\Merchant::where('status', 0)->select();
        $config = web_config('merchant_config');
        foreach ($list as $item) {
            $arr = calc_time_diff($item['create_time'], date_now());
            if ($arr['day'] >= $config['merch_logout_deadline']) {
                model\Merchant::where('id', $item['id'])->update([
                    'update_time' => date_now(),
                    'delete_time' => date_now(),
                ]);
                model\Store::where('merchant_id', $item['id'])->update([
                    'update_time' => date_now(),
                    'delete_time' => date_now(),
                ]);
            }
        }
        makelog('---注销未填写资料商户---end---' . time(), 'crontab/cronjob');
    }

}